<template>
  <div>
    <!--    搜索表单-->
    <div style="margin-bottom: 20px">
      <el-input style="width: 240px" placeholder="请输入名称" v-model="params.name"></el-input>
      <el-input style="width: 240px; margin-left: 5px" placeholder="请输入联系方式" v-model="params.phone"></el-input>
      <el-button style="margin-left: 5px" type="primary" @click="initUser"><i class="el-icon-search"></i> 搜索</el-button>
      <el-button style="margin-left: 5px" type="warning" @click="reset"><i class="el-icon-refresh-right"></i> 重置
      </el-button>
    </div>

    <el-table :data="tableData" stripe>
      <el-table-column prop="id" label="编号"></el-table-column>
      <el-table-column prop="username" label="会员卡号"></el-table-column>
      <el-table-column prop="name" label="名称"></el-table-column>
      <el-table-column prop="age" label="年龄"></el-table-column>
      <el-table-column prop="address" label="地址"></el-table-column>
      <el-table-column prop="phone" label="联系方式"></el-table-column>
      <el-table-column prop="sex" label="性别"></el-table-column>
      <el-table-column prop="account" label="账户积分"></el-table-column>
      <el-table-column prop="status" label="状态">
        <template v-slot="scope">
          <el-switch
              v-model="scope.row.status"
              active-color="#13ce66"
              @change="statusChange(scope.row)"
              inactive-color="#ff4949">
          </el-switch>
        </template>
      </el-table-column>
      <el-table-column label="操作" width="300">
        <template v-slot="scope">
          <!--          scope.row 就是当前行数据-->
                  <el-button type="warning" @click="handleAccountAdd(scope.row)">充值</el-button>
          <el-button type="primary" @click="$router.push('/editUser?id=' + scope.row.id)">编辑</el-button>
                  <el-popconfirm
                      style="margin-left: 5px"
                      title="您确定删除这行数据吗？"
                      @confirm="del(scope.row.id)"
                  >
                    <el-button type="danger" slot="reference">删除</el-button>
                  </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>

    <el-dialog title="充值账户" :visible="dialogFormVisible" width="40%" >
      <el-form :model="form" style="width: 600px" :rules="rules" ref="dialogForm">
        <el-form-item label="账户积分"  prop="account">
          <el-input v-model="form.account" disabled ></el-input>
        </el-form-item>
        <el-form-item label="充值积分"  prop="score">
          <el-input v-model="form.score" ></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="addAccount">确 定</el-button>
      </div>
    </el-dialog>

    <!--    分页-->
    <div style="margin-top: 20px">
      <el-pagination
          background
          :curren-page="params.pageNum"
          :page-size="params.pageSize"
          layout="total, prev, pager, next"
          @current-change="handleCurrentChange"
          :total="total">
      </el-pagination>
    </div>
  </div>
</template>

<script>
import request from "@/utils/request";

export default {
  name: 'User',
  components: {},
  data() {
    const checkAccount = (rule, value, callback) => {
      var num=parseInt(value);
      if (num<0||num>200) {
        callback(new Error('请输入 0-200 的数字'));
      }
      callback()
    };
    return {
      rules:{
        score: [
          { validator: checkAccount, trigger: 'blur' }
        ]
      },
      form:{},
      dialogFormVisible:false,
      tableData: [],
      total: 0,
      params: {
        pageSize: 10,
        pageNum: 1,
        name: '',
        phone: ''
      }
    }
  },
  created() {
    this.initUser()
  },
  methods: {
    statusChange(row){
      request.put("/user/",row).then(res=>{
        if(res.code=='200'){
          this.$notify.success("修改成功")
          this.initAdmin()
        }else{
          this.$notify.error("修改失败")
        }
      })
    },
    addAccount(){
      this.$refs['dialogForm'].validate((valid) => {
        if(valid){
          request.put("/user/account",this.form).then(res=>{
            if(res.code=='200'){
              this.$notify.success("充值成功");
              this.$refs['dialogForm'].resetFields()
              this.dialogFormVisible=false;
              this.initUser()
            }else{
              this.$notify.error(res.msg)
            }
          })
        }
      })
    },
    handleAccountAdd(row){
      this.dialogFormVisible=true
        this.form=row;
    },
    initUser() {
      request.get("/user/page", {
        params: this.params
      }).then(res => {
        if (res.code == '200') {
          this.tableData = res.data.list
          this.total = res.data.total
        }
      })
    },
    reset() {
      this.params = {
        pageSize: 10,
        pageNum: 1,
        name: '',
        phone: ''
      }
      this.initUser()
    },
    handleCurrentChange(pageNum) {
      this.params.pageNum = pageNum
      this.initUser()
    },
    del(id) {
      request.delete("/user/"+id).then(res=>{
        if(res.code=='200'){
          this.$notify.success("删除成功")
          this.initUser()
        }
      })
    }
  }
}
</script>

<style scoped>

</style>